package cn.mynb.web.user;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import cn.mynb.entity.UserBean;
import cn.mynb.service.impl.UserServiceImpl;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	
	/**
	 * user action class
	 */
	private static final long serialVersionUID = 1L;
	
	private String username;
	
	private String password;
	
	private UserBean user;

	private String message;
	
	private List<UserBean> userlist;

	/**
	 * Author: 鱼
	 * 保存会员   会员注册提交信息时保存数据
	 * 参数：user 会员对象
	 */
	@SuppressWarnings("unchecked")
	public String saveUser(){
        @SuppressWarnings("unused")
		HttpServletRequest request = ServletActionContext.getRequest();
        @SuppressWarnings("unused")
		HttpServletResponse response = ServletActionContext.getResponse(); 
        System.out.println("注册中……");
        /* 会员类型有三种
         * 超级管理员:super manager
         * 管理员:manager
         * 普通会员：register
         */
        int rs = 0;
        if( this.user!=null ){
        	String [] pname = new String[2];
        	pname[0] = "username";
        	pname[1] = this.user.getUsername();
        	
        	String[] pemail =  new String[2];
        	pemail[0] = "useremail";
        	pemail[1] = this.user.getUseremail();
        	
        	UserBean ub_name = new UserServiceImpl().querySingleUserService(pname); //检查用户名是否存在
        	UserBean ub_email = new UserServiceImpl().querySingleUserService(pemail); //检查email是否存在
        	
        	//当用户名跟email 不存在时，保存用户
        	if(null == ub_name && ub_email==null ){
		        this.user.setUsertype("register");   //会员类型 
		        this.user.setBlock(0); //会员状态, 0 正常，1 被屏蔽掉
		        this.user.setRegisterDate(new Date());
				rs = new UserServiceImpl().saveUserService(user);
        	}else{
        		this.setMessage("用户名或email已经被注册...");
        		System.out.println("用户名或email已经被注册...");
        	}
        }
        
        if( rs==1 ){
        	return SUCCESS; 	
        }else{
        	return ERROR;
        }
        
	}
	
	/**
	 * author:鱼
	 * 检查用户名或email是否存在
	 * Date: 2011-4-11
	 */
	public void checkUser(){
		
		HttpServletResponse res = ServletActionContext.getResponse();
		res.setContentType("application/json;chartset=utf-8");
		PrintWriter out = null;
		System.out.println("进入checkUser action");
		try{
			out  = res.getWriter();
			out.write("{total:100}");
			out.flush();
			
		}catch(Exception e){
			e.printStackTrace();
		}	
	}
	
	/**
	 * author:鱼
	 * Date: 2010-11-26
	 * 管理用户列表
	 * @return
	 */
	public String userList(){
		
		userlist = new UserServiceImpl().queryAllUserService();
		System.out.println(userlist.size());
		return SUCCESS;
	}
	
	/**
	 * 删除单个会员
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String deleteUser(){
		
		
		@SuppressWarnings("unused")
		int result = new UserServiceImpl().deleteUserService(user);
		userlist   = new UserServiceImpl().queryAllUserService();
		return SUCCESS;
	}
	
	public List<UserBean> getUserlist() {
		return userlist;
		
	}

	public void setUserlist(List<UserBean> userlist) {
		this.userlist = userlist;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public UserBean getUser() {
		return user;
	}

	public void setUser(UserBean user) {
		this.user = user;
	}
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}
